﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
using QuanLyKhachSan.BusinessObject;
using QuanLyKhachSan.DataAccess;
namespace QuanLyKhachSan.BusinessLogic
{
    class ChiTietDVBL
    {
        public ChiTietDVBO ctdvBO = new ChiTietDVBO();
        ConnectData connData = new ConnectData();
        public DataTable LayDSChiTietDV(){
            string sql = "LayChiTietDV";
            return connData.GetDataTable(sql);
        }
        
        //lay ma dich vu
        public static string[,] LaymaDichVu(string maThuePhong)
        {
            ConnectData connData1 = new ConnectData();
            SqlConnection conn = connData1.Connect1();
            conn.Open();
            SqlCommand objCmd = new SqlCommand("select MaDichVu, SoLuong from ChiTietDV where MaThuePhong='" + maThuePhong + "'", conn);
            SqlDataReader dr = objCmd.ExecuteReader();
            string[,] maDichVu = new string[100,100];
            int count = 0;
            while (dr.Read())
            {
                maDichVu[count, 0] = dr.GetValue(0).ToString();         
                maDichVu[count, 1] = dr.GetValue(1).ToString();
                count++;
            }
            conn.Close();
            return maDichVu;
        }


        //Tim kiem
        public DataTable TimKiemChiTietDV(string timKiem)
        {
            string sql = "select * from ChiTietDV where MaChiTietDV like '%" + timKiem + "%' or NgaySuDung like '%" + timKiem + "%' or MaThuePhong like '%" + timKiem + "%'";
            return connData.GetDataTable(sql);
        }


        //thêm mới chi tiết dịch vụ
        public void ThemCTDV(ChiTietDVBO ctdv) {
           
                //string sql = string.Format("INSERT INTO ChiTietDV (MaChiTietDV, MaDichVu, NgaySuDung, MaThuePhong, SoLuong) "
                //     + "VALUES ('{0}','{1}','{2}','{3}','{4}')", ctdv.MaChiTietDV, ctdv.MaDichVu, ctdv.NgaySuDung, ctdv.MaThuePhong,ctdv.SoLuong);
                //if (connData.ExeCuteQuery(sql))
                //{
                //    MessageBox.Show("Thêm mới dữ liệu thành công!","Thông Báo",MessageBoxButtons.OK,MessageBoxIcon.Information);
                  
                //}
            SqlConnection conn = connData.Connect1();
            conn.Open();
            string sql = "ThemMoiChiTietDV";
            SqlCommand objCmd = new SqlCommand(sql, conn);
            objCmd.CommandType = CommandType.StoredProcedure;
            objCmd.Parameters.Add("@MaChiTietDV", SqlDbType.NVarChar, 30);
            objCmd.Parameters.Add("@MaDichVu", SqlDbType.NVarChar, 30);
            objCmd.Parameters.Add("@NgaySuDung", SqlDbType.DateTime);
            objCmd.Parameters.Add("@MaThuePhong", SqlDbType.NVarChar, 30);
            objCmd.Parameters.Add("@SoLuong", SqlDbType.Int);

            objCmd.Parameters["@MaChiTietDV"].Value = ctdv.MaChiTietDV;
            objCmd.Parameters["@MaDichVu"].Value = ctdv.MaDichVu;
            objCmd.Parameters["@NgaySuDung"].Value = ctdv.NgaySuDung;
            objCmd.Parameters["@MaThuePhong"].Value = ctdv.MaThuePhong;
            objCmd.Parameters["@SoLuong"].Value = ctdv.SoLuong;
            objCmd.ExecuteNonQuery();
            conn.Close();
        }

        public void SuaCTDV(ChiTietDVBO ctdv)
        {

            //string sql = string.Format("UPDATE ChiTietDV SET MaDichVu = '{0}', NgaySuDung ='{1}', MaThuePhong='{2}', SoLuong = '{3}' WHERE MaChiTietDV = '{4}' ",
            //                             ctdv.MaDichVu, ctdv.NgaySuDung, ctdv.MaThuePhong,ctdv.SoLuong, ctdv.MaChiTietDV);
            //if (connData.ExeCuteQuery(sql))
            //{
            //    MessageBox.Show("sua nhan vien thanh cong", "thong bao", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //}
            SqlConnection conn = connData.Connect1();
            conn.Open();
            string sql = "SuaChiTietDV";
            SqlCommand objCmd = new SqlCommand(sql, conn);
            objCmd.CommandType = CommandType.StoredProcedure;
            objCmd.Parameters.Add("@MaChiTietDV", SqlDbType.NVarChar, 30);
            objCmd.Parameters.Add("@MaDichVu", SqlDbType.NVarChar, 30);
            objCmd.Parameters.Add("@NgaySuDung", SqlDbType.DateTime);
            objCmd.Parameters.Add("@MaThuePhong", SqlDbType.NVarChar, 30);
            objCmd.Parameters.Add("@SoLuong", SqlDbType.Int);

            objCmd.Parameters["@MaChiTietDV"].Value = ctdv.MaChiTietDV;
            objCmd.Parameters["@MaDichVu"].Value = ctdv.MaDichVu;
            objCmd.Parameters["@NgaySuDung"].Value = ctdv.NgaySuDung;
            objCmd.Parameters["@MaThuePhong"].Value = ctdv.MaThuePhong;
            objCmd.Parameters["@SoLuong"].Value = ctdv.SoLuong;
            objCmd.ExecuteNonQuery();
            conn.Close();

        }
        //
        public string NextID()
        {
            return Utilities.NextID(connData.GetLastID("ChiTietDV", "MaChiTietDV"), "CTDV");
        }

        public bool CheckExists(string mactdv)
        {
            if (connData.CheckExistValue("ChiTietDV", "MaChiTietDV", mactdv))
            {
                return true;
            } return false;
        }

        //xóa
        public void Delete(string ctdv)
        {
            string sql = "Delete from ChiTietDV where MaChiTietDV='" + ctdv + "'";
            connData.ExeCuteQuery(sql);
        }
    }
}
